package view
{
	import flash.events.MouseEvent;
	
	import mx.controls.Alert;
	import mx.rpc.events.ResultEvent;
	import mx.utils.StringUtil;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import services.mainservice.MainService;
	import services.userservice.UserService;
	
	import util.MD5;
	
	import valueObjects.Contact;
	import valueObjects.User;
	
	public class UserMediator extends Mediator implements IMediator
	{
		public static const USER_MEDIATOR:String = "USER_MEDIATOR";
		private var type:int;
		private var user:User;
		
		public function UserMediator(viewComponent:Object=null,type:int=0,user:User=null)
		{
			this.type = type;
			this.user = user;
			super(USER_MEDIATOR, viewComponent);
			this.userView.type = type;
			this.userView.user = user;
			this.userView.create.addEventListener(MouseEvent.CLICK,onClick);
		}
		
		private function onClick(evt:MouseEvent):void {
			switch(type) {
				case 0:
					onCreate(evt);
					break;
				case 1:
					onEdit(evt);
					break;
				case 2:
					break;
			}
		}
		
		private function onCreate(evt:MouseEvent):void {
			if(StringUtil.trim(this.userView.username.text)!="" &&
				StringUtil.trim(this.userView.password.text)!="") {
				
				var service:UserService = new UserService();
				var user:User = new User();
				user.username = StringUtil.trim(this.userView.username.text);
				user.password = MD5.hash(StringUtil.trim(this.userView.password.text));
				
				service.addEventListener(ResultEvent.RESULT,userAdded);
				service.addUser(user);
				this.userView.closeWindow();
			}else {
				Alert.show("不能有空字段");
			}
		}
		
		private function userAdded(evt:ResultEvent):void {
			var result:int = evt.result as int;
			if(result>0) {
				Alert.show("添加成功");
				this.sendNotification(EasyMediator.CHANGE_VIEW);
			}else {
				Alert.show("添加失败");
			}
		}
		
		private function onEdit(evt:MouseEvent):void {
			if(StringUtil.trim(this.userView.username.text)!="" &&
				StringUtil.trim(this.userView.password.text)!="") {
				
				var service:UserService = new UserService();
				var user:User = new User();
				user.id = this.user.id;
				user.username = StringUtil.trim(this.userView.username.text);
				user.password = MD5.hash(StringUtil.trim(this.userView.password.text));
				
				service.addEventListener(ResultEvent.RESULT,userEdited);
				service.updateUser(user);
				this.userView.closeWindow();
			}else {
				Alert.show("不能有空字段");
			}
		}
		
		private function userEdited(evt:ResultEvent):void {
			var result:int = evt.result as int;
			if(result>0) {
				Alert.show("编辑成功");
				this.sendNotification(EasyMediator.CHANGE_VIEW);
			}else {
				Alert.show("编辑失败");
			}
		}
		
		override public function handleNotification(notification:INotification):void {
			switch(notification.getName()) {
			}
		}
		
		public function get userView():UserView {
			return this.viewComponent as UserView;
		}
	}
}